﻿using UnityEngine;
using System.Collections;

public class Achievement : MonoBehaviour
{

    public Texture texture;

    [SerializeField]
    private int xpReward;
    public int XpReward
    {
        get { return xpReward; }
        set { xpReward = value; }
    }

    [SerializeField]
    private int amountToTrigger;
    public int AmountToTrigger
    {
        get { return amountToTrigger; }
        set { amountToTrigger = value; }
    }


    [SerializeField]
    private string achievName;
    public string AchievName
    {
        get { return achievName; }
        set { achievName = value; }
    }

    [SerializeField]
    private string description;
    public string Description
    {
        get { return description; }
        set { description = value; }
    }

    private bool showing;
    private bool stop;
    private bool triggered;
    public bool Triggered
    {
        get { return triggered; }
    }

    private Rect rect;
    private float deltaTranslation;
    private float pauseTime;
    private float startPauseTime;
    int width;
    int height;

	// Use this for initialization
	void Start () {
        showing = true;
        triggered = false;
        stop = false;
        deltaTranslation = 0;
        width = 200;
        height = 70;
        pauseTime = 2;
        rect = new Rect(Screen.width, Screen.height / 10, width, height);
	}
	
	// Update is called once per frame
	void Update () {
        if (triggered && !stop)
        {
            if (deltaTranslation < width + 5 && showing)
            {
                deltaTranslation += Time.deltaTime * 200;
                startPauseTime = Time.time;
            }
            else
            {
                if(Time.time - startPauseTime > pauseTime)
                {
                    showing = false;
                    if(deltaTranslation < 0)
                    {
                        stop = true;
                    }
                    else
                    {
                        deltaTranslation -= Time.deltaTime * 200;
                    }
                }
            }
        }    
	}

    public void trigger()
    {
        triggered = true;
        EventManager<int, Vector3>.Raise("xp gain", XpReward, Vector3.zero);
    }

    void OnGUI()
    {
        if (triggered && !stop)
        {
            GUI.Box(new Rect(rect.x - deltaTranslation, rect.y, rect.width, rect.height), new GUIContent(""));
            GUI.DrawTexture(new Rect(rect.x - deltaTranslation + 5, rect.y - (texture.height - height) /2, texture.width, texture.height), texture);
            GUI.Label(new Rect(rect.x + texture.width + 10 - deltaTranslation, rect.y + rect.height / 6, rect.width - (texture.width + 10), rect.height), achievName);
            GUI.Label(new Rect(rect.x + texture.width + 10 - deltaTranslation, rect.y + rect.height / 2, rect.width - (texture.width + 10), rect.height), Description);
        }    
    }
}
